#include "INIConfigFile.h"

#include <fstream>

#ifdef _DEBUG
	#include <jmtools/LogManager.h>
	#include <iostream>

	using namespace JMTools::Logs;
#endif

namespace JMTools
{
	namespace Config
	{
		INIConfigFile::INIConfigFile(std::string name):ConfigFile(name)
		{
		}
	
		INIConfigFile::~INIConfigFile()
		{
		}

		bool INIConfigFile::load()
		{
			std::ifstream file(this->name.data());
			if (file.is_open())
			{
#ifdef _DEBUG
				LogManager::getInstance().getLog("JMTools_Config.log")->logMessage("Loading "+this->name+"...",LT_INFORMATION);
#endif
				char buffer[1024];
				file.getline(buffer,1024);
				std::string data = buffer;
				ConfigGroup * cg = 0;
				while (!file.eof())
				{
					if (data.find('#')!=0)
					{
						if(data.find('#')<data.size())
							data.erase(data.find('#'));
						if(data.find('[')<data.size())
						{
							std::string group = data.substr(data.find('[')+1,data.find(']')-1);
#ifdef _DEBUG
							LogManager::getInstance().getLog("JMTools_Config.log")->logMessage("Adding group "+group+"...",LT_INFORMATION);
#endif
							cg = new ConfigGroup(group);
							this->groups.push_back(cg);
						}
						else
						{
							if (cg == 0)
							{
								cg = new ConfigGroup();
								this->groups.push_back(cg);
							}
							std::string key = data.substr(0,data.find('='));
							std::string value = data.substr(data.find('=')+1);
							if (value.find_last_of(' ')<value.size())
								value.erase(value.find_last_of(' '));
#ifdef _DEBUG
							LogManager::getInstance().getLog("JMTools_Config.log")->logMessage("Adding value "+value+" in key "+key+"...",LT_INFORMATION);
#endif
							cg->add(key,value);
						}
					}
					file.getline(buffer,1024);
					data = buffer;
				}
#ifdef _DEBUG
				LogManager::getInstance().getLog("JMTools_Config.log")->logMessage("Loaded "+this->name+"...",LT_INFORMATION);
#endif
				file.close();
				return true;
			}
#ifdef _DEBUG
			LogManager::getInstance().getLog("JMTools_Config.log")->logMessage("Couldn't load " + this->name + ".",LT_WARNING);
#endif
			if (file.is_open())
				file.close();
			return false;
		}

		void INIConfigFile::save()
		{
			std::ofstream file(this->name.data());
#ifdef _DEBUG
			LogManager::getInstance().getLog("JMTools_Config.log")->logMessage("Saving "+this->name+"...",LT_INFORMATION);
#endif
			std::list<ConfigGroup *>::iterator it;
			for (it = this->groups.begin(); it!=this->groups.end(); it++)
			{
#ifdef _DEBUG
				LogManager::getInstance().getLog("JMTools_Config.log")->logMessage("Adding group "+(*it)->getName()+"...",LT_INFORMATION);
#endif
				file<<"["<<(*it)->getName()<<"]"<<std::endl;
				std::map<std::string,std::list<std::string> >::reverse_iterator itm;
				std::map<std::string,std::list<std::string> > data = (*it)->getData();	
				for (itm = data.rbegin(); itm != data.rend(); itm++)
				{
					std::list<std::string>::iterator itl;
					for (itl = (*itm).second.begin(); itl != (*itm).second.end(); itl++)
						file<<(*itm).first<<"="<<*itl<<std::endl;
				}
			}
			if (file.is_open())
				file.close();
#ifdef _DEBUG
			LogManager::getInstance().getLog("JMTools_Config.log")->logMessage(this->name+" saved.",LT_INFORMATION);
#endif
		}
	}
}
